package org.mspring.platform.api.rss.writers.defaults;

import java.util.Iterator;
import java.util.List;

import org.mspring.platform.api.rss.elements.Channel;
import org.mspring.platform.api.rss.elements.Image;
import org.mspring.platform.api.rss.elements.Item;
import org.mspring.platform.api.rss.elements.TextInput;
import org.mspring.platform.api.rss.writers.GenericRSSWriter;

import electric.xml.Document;
import electric.xml.Element;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月26日
 */
@SuppressWarnings({ "rawtypes" })
public class RSS092 extends GenericRSSWriter {
    public String getFeed(Channel paramChannel) {
        Document localDocument = new Document();
        Element localElement1 = localDocument.newRoot();
        Element localElement2 = localElement1.addElement("channel");
        TextInput localTextInput = paramChannel.getTextInput();
        Image localImage = paramChannel.getImage();
        List localList = paramChannel.getItems();
        localElement1.setName("rss");
        addAttribute(localElement1, "version", getVersion());
        GenericRSSWriter.addElement(localElement2, "title", paramChannel.getTitle());
        GenericRSSWriter.addElement(localElement2, "link", paramChannel.getLink());
        GenericRSSWriter.addElement(localElement2, "description", paramChannel.getDescription());
        GenericRSSWriter.addElement(localElement2, "docs", getDocs());
        addExtrasToChannel(localElement2, paramChannel);
        if (localImage != null)
            addImage(localElement2, localImage);
        if (localTextInput != null)
            addTextInput(localElement2, localTextInput);
        if ((localList != null) && (localList.size() > 0)) {
            Iterator localIterator = localList.iterator();
            while (localIterator.hasNext()) {
                Item localItem = (Item) localIterator.next();
                addItem(localElement2, localItem);
            }
        }
        return localDocument.toString();
    }

    protected String getDocs() {
        return "http://backend.userland.com/rss092";
    }

    protected String getVersion() {
        return "0.92";
    }

    protected void addExtrasToChannel(Element paramElement, Channel paramChannel) {
    }

    protected Element addItem(Element paramElement, Item paramItem) {
        Element localElement = null;
        if (paramItem != null) {
            localElement = paramElement.addElement("item");
            GenericRSSWriter.addElement(localElement, "title", paramItem.getTitle());
            GenericRSSWriter.addElement(localElement, "link", paramItem.getLink());
            GenericRSSWriter.addElement(localElement, "description", paramItem.getDescription());
            addExtrasToItem(localElement, paramItem);
        }
        return localElement;
    }

    protected void addExtrasToItem(Element paramElement, Item paramItem) {
    }

    protected Element addTextInput(Element paramElement, TextInput paramTextInput) {
        Element localElement = null;
        if (paramTextInput != null) {
            localElement = paramElement.addElement("textInput");
            GenericRSSWriter.addElement(localElement, "title", paramTextInput.getTitle(), true);
            GenericRSSWriter.addElement(localElement, "link", paramTextInput.getLink(), true);
            GenericRSSWriter.addElement(localElement, "description", paramTextInput.getDescription(), true);
            GenericRSSWriter.addElement(localElement, "name", paramTextInput.getName(), true);
        }
        return localElement;
    }

    protected Element addImage(Element paramElement, Image paramImage) {
        Element localElement = null;
        if (paramImage != null) {
            localElement = paramElement.addElement("image");
            GenericRSSWriter.addElement(localElement, "title", paramImage.getTitle());
            GenericRSSWriter.addElement(localElement, "link", paramImage.getLink());
            GenericRSSWriter.addElement(localElement, "url", paramImage.getUrl());
        }
        return localElement;
    }
}